Component({
  properties: {
    visible: {
      type: Boolean,
      value: false
    },
    record: {
      type: Object,
      value: null
    }
  },

  data: {
    qrcodeUrl: ''
  },

  observers: {
    'visible, record': function(visible, record) {
      if (visible && record) {
        this.generateQrcode();
      }
    }
  },

  methods: {
    generateQrcode() {
      const { record } = this.data;
      if (!record) return;

      this.setData({ qrcodeUrl: '' });
      
      // 调用后端接口生成二维码
      wx.request({
        url: 'https://api.example.com/qrcode/generate',
        method: 'POST',
        data: {
          recordId: record.id,
          boatNumber: record.boatNumber,
          time: record.time,
          location: record.location,
          fisher: record.fisher
        },
        success: (res) => {
          if (res.data.success) {
            this.setData({
              qrcodeUrl: res.data.qrcodeUrl
            });
          } else {
            wx.showToast({
              title: '生成二维码失败',
              icon: 'none'
            });
          }
        },
        fail: () => {
          wx.showToast({
            title: '网络错误',
            icon: 'none'
          });
        }
      });
    },

    onClose() {
      this.triggerEvent('close');
    },

    saveQrcode() {
      const { qrcodeUrl } = this.data;
      if (!qrcodeUrl) return;

      wx.saveImageToPhotosAlbum({
        filePath: qrcodeUrl,
        success: () => {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          });
        },
        fail: (err) => {
          if (err.errMsg.includes('auth deny')) {
            wx.showModal({
              title: '提示',
              content: '需要您授权保存图片到相册',
              success: (res) => {
                if (res.confirm) {
                  wx.openSetting();
                }
              }
            });
          } else {
            wx.showToast({
              title: '保存失败',
              icon: 'none'
            });
          }
        }
      });
    }
  }
}); 